package main.java.message;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import main.java.regression.User;

import org.json.simple.JSONObject;

public class Message {
	public static final int MAX = 10000;
	JSONObject obj;
	String id;
	User reciever;

	public Message(MessageContents messageType) {
		obj = new JSONObject();
		setId(generateID());
		put(MessageTags.TYPE.getTag(), messageType.getContent());
		put(MessageTags.ID.getTag(), getId());
	}

	public String generateID() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss-");
		return dateFormat.format(new Date()) + (new Random()).nextInt() % MAX;
	}

	public JSONObject getObj() {
		return obj;
	}

	public void setObj(JSONObject obj) {
		this.obj = obj;
	}

	public User getReciever() {
		return reciever;
	}

	public void setReciever(User reciever) {
		this.reciever = reciever;
	}

	@SuppressWarnings("unchecked")
	public void put(String tag, String content) {
		obj.put(tag, content);
	}

	public void put(MessageTags tag, String content) {
		put(tag.getTag(), content);
	}

	public String toString() {
		return obj.toJSONString();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
